package com.yztc.love_travel.adater;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.love_travel.R;
import com.yztc.love_travel.bean.BuyingBean;
import com.yztc.love_travel.constant.URLConstant;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by My on 2016/11/29.
 */

public class BuyingAdapter extends BaseAdapter {

    private List<BuyingBean.ManualArticleListBean> manualArticleList;
    private Context context;

    public BuyingAdapter(List<BuyingBean.ManualArticleListBean> manualArticleList, Context context) {
        this.manualArticleList = manualArticleList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return manualArticleList.size();
    }

    @Override
    public Object getItem(int position) {
        return manualArticleList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_layout_buy, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (BuyingAdapter.ViewHolder) convertView.getTag();
        }
        BuyingBean.ManualArticleListBean dataBean = manualArticleList.get(position);
        vh.textviewTitleBuy.setText(dataBean.getTitle());
        vh.textviewContentBuy.setText(dataBean.getContent());
        String coverFoto = dataBean.getCoverFoto();
        String url = URLConstant.Buying.URL_BUYING_PICTURE + coverFoto;
        if (!TextUtils.isEmpty(coverFoto)) {
            Glide.with(context).load(url)
                    .placeholder(R.mipmap.logo)
                    .error(R.mipmap.logo)
                    .into(vh.imageviewBuy);
        }
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.imageview_buy)
        ImageView imageviewBuy;
        @BindView(R.id.textview_title_buy)
        TextView textviewTitleBuy;
        @BindView(R.id.textview_content_buy)
        TextView textviewContentBuy;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
